<?php
class InfoPageantAddAction extends BaseAction 
{
	public $name_chinese;
	public $name_english;
	public $nickname;
	
	/**
	 * @maxLength 9
	 * @label Pageant No.
	 */
	public $pageant_no;
	public $gender;
	public $date_of_birth_year;
	public $date_of_birth_month;
	public $date_of_birth_day;
	public $country;
	public $province_state;
	public $origin_city;
	public $origin_province_state_prefecture;		
	public $origin_country;
	public $astrology;
	public $zodiac;
	public $occupation;
	public $education;
	public $height;
	public $weight;
	public $measurements;
	public $aspiration;
	public $characters;
	public $language_spoken;
	public $hobbies;
	public $talent;
	public $description;
	public $remark;
	
	/**
	 * @filter datetime
	 * @label Publish Date/Time
	 */
    public $publish_datetime;
	
	/**
	 * @filter datetime
	 * @label Expire Date/Time
	 */
    public $expire_datetime;
	
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;

	public $programme_id;
	public $onair_episode_no;

	public $programme_title_cn;
	public $programme_title_en;
	public $programme_path;
	public $country_name;
	public $province_name;
	public $province_state_prefecture;

	public $ut;
	public $reset;
	
	public function execute()
	{
		if (!is_null($this->reset)) {
			$this->setData('type', 'pageant');
			$this->setData('act', 'add');
			return 'reset';
		}

		try {
			if(count($this->_errors) > 0) {
				$this->init_page_data();
				return 'filter-error';
			}

			if(!CommonUtils::check_date($this->date_of_birth_year, $this->date_of_birth_month, $this->date_of_birth_day)) {
				$this->init_page_data();
				$this->addError('pageant.Date of Birth', array('0005', 'Date of Birth'));
				return 'filter-error';
			}

			//check name unique
			if (is_null($this->name_chinese) && is_null($this->name_english)) {
				$this->init_page_data();
				$this->addError('Pageant.Name', array('0044', 'Name'));
				return 'filter-error';
			}

			//check programme and on air episode no
			$check_exists = episode::getEpisodeID($this->programme_id, $this->onair_episode_no);
			if ($check_exists == '') {
				$this->addError('Programme or Episode', array('0103', 'Programme or Episode'));
				$this->init_page_data();
				return 'logic-error';
			}
			
            //check publish_datetime && expire_datetime
            if (empty($this->publish_datetime)
                || empty($this->expire_datetime)
                || $this->publish_datetime > $this->expire_datetime) {

                $this->init_page_data();
                $this->addError('Publish Date/Time && Expire Date/Time', array('0033', 'Publish Date/Time && Expire Date/Time'));
                return 'filter-error';
            }

			$insert_id = $this->add_info_pageant();
			if (FALSE != $insert_id) {
				$this->setData('type', 'pageant');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			return "succ";
		} catch(Exception $e) {
			error_log("Add pageant Fails: " . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			return 'system-error';
		}
	}    

	/**
	 * add cast base information
	 */
	private function add_info_pageant()
	{
		$info_pageant = new info_pageant();
		$info_pageant->set($this);
		$info_pageant->episode_id = episode::getEpisodeID($this->programme_id, $this->onair_episode_no);
		$info_pageant->created_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_pageant->created_by = CommonUtils::get_user();
        $info_pageant->modified_at = $info_pageant->created_at;
        $info_pageant->modified_by = $info_pageant->created_by;
		$info_pageant->status = AC::STATUS_NEW;
		$info_pageant->origin_province_state_prefecture = $this->province_state;
		$info_pageant->origin_country = $this->country;
		$insert_id = Model::$dbo['w']->save($info_pageant);
		if ($insert_id > 0) {
            $primary_key = array(
                'id'    => $insert_id
            );
            $fields = array(
                'name_chinese',
                'name_english',
                'nickname',
                'origin_city',
                'occupation',
                'education',
                'height',
                'weight',
                'measurements',
                'aspiration',
                'characters',
                'language_spoken',
                'hobbies',
                'talent',
                'description',
                'remark',
            );
            CommonUtils::filterC2A0('info_pageant', $primary_key, $fields);

			$this->addInfo("info_pageant:".AC::LOG_OP_CREATE.":".$insert_id, 
						   "add info_pageant success", 
						   json_encode($info_pageant));
			return $insert_id;
		} else {
			$this->addError("info_pageant:".AC::LOG_OP_CREATE.":".$insert_id, 
							"add info_pageant error", 
							json_encode($info_pageant));
			return false;
		}
	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$pageant = new info_pageant();
		$ref = new ReflectionObject($this);
		$props = $ref->getProperties();
		foreach ($props as $prop) {
			if ($prop->isPublic()) {
				$prop_name = $prop->getName();
				$pageant->$prop_name = $this->$prop_name;
			}
		}
		$pageant->country_name = country::getCountryName($this->country);
		$pageant->origin_country = !empty($this->country) ? $this->country : '';
		$pageant->province_name  = province_state_prefecture::getProvinceName($this->province_state);
		$pageant->origin_province_state_prefecture = !empty($this->province_state) ? $this->province_state : '';
		$this->setData('pageant', $pageant);

		$this->setData('publishLog', '');

		//jump page
		$this->setData('type', 'pageant');
		$this->setData('act', 'add');
		$this->setData('step', 1);
	}
}
?>
